/*
 * @Description:
 * @version:
 * @Author: dh
 * @Date: 2022-02-23 10:26:57
 * @LastEditors: dh
 * @LastEditTime: 2024-01-17 10:12:20
 */
let express = require('express')
let app = express()
let whitList = ['http://127.0.0.1:5605'] //设置白名单
app.use(function (req, res, next) {
	let origin = req.headers.origin
	if (whitList.includes(origin)) {
		// 设置哪个源可以访问我
		res.setHeader('Access-Control-Allow-Origin', origin)
		// 允许携带哪个头访问我
		res.setHeader('Access-Control-Allow-Headers', 'name')
		// 允许哪个方法访问我
		res.setHeader('Access-Control-Allow-Methods', 'PUT')
		// 允许携带cookie
		res.setHeader('Access-Control-Allow-Credentials', true)
		// 预检的存活时间
		res.setHeader('Access-Control-Max-Age', 6)
		// 允许返回的头
		res.setHeader('Access-Control-Expose-Headers', 'name')
		// if (req.method === 'OPTIONS') {
		// 	res.end() // OPTIONS请求不做任何处理
		// }
	}
	next()
})
app.put('/getData', function (req, res) {
	// console.log(req.headers)
	res.setHeader('name', 'jw')
	res.send({
		msg: '我不爱你',
	})
})
app.get('/getData', function (req, res) {
	// console.log(req.headers)
	res.send({
		msg: '我不爱你',
	})
})
app.use(express.static(__dirname))
app.listen(4000, () => {
	console.log('http://localhost:4000')
})
